﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using Kaiser.Modules.Core;
using Kaiser.Modules.Core.Aggregates.Bills;
using Kaiser.Modules.Core.Aggregates.Enum;
using Kaiser.Modules.Core.Aggregates.Product;
using Kaiser.Modules.Core.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Kaiser.Modules.Core.Aggregates;

namespace Kaiser.Tests
{
    [TestClass]
    public class UnitTest1
    {
        private static Account curUser;
        public void InitDate()
        {

             curUser = CoreServiceFactory.CreateUserService().SignIn("admin", "admin");

            #region 初始化收支项目

            using (Balance model = Balance.Create())
            {
                model.BalanceName = "销售收入";
                model.BalanceType = BalanceTypeEnum.收入;
                model.IsInvolvedProfit = true;
                Balance.Add(model);
            }

            using (Balance model = Balance.Create())
            {
                model.BalanceName = "收定金/欠款";
                model.BalanceType = BalanceTypeEnum.收入;
                model.IsInvolvedProfit = true;
                Balance.Add(model);
            }

            using (Balance model = Balance.Create())
            {
                model.BalanceName = "付定金/欠款";
                model.BalanceType = BalanceTypeEnum.支出;
                model.IsInvolvedProfit = true;
                Balance.Add(model);
            }

            using (Balance model = Balance.Create())
            {
                model.BalanceName = "进货支出";
                model.BalanceType = BalanceTypeEnum.支出;
                model.IsInvolvedProfit = true;
                Balance.Add(model);
            }

            #endregion

            #region 初始化账户

            using (var model = new SettleAcct())
            {
                model.BankName = "建设银行";
                model.InitAmount = 0;
                model.CurAmount = 0;
                model.IsSystem = true;
                model.CreaDateTime = DateTime.Now;
                model.SettleAcctName = "现金";

                SettleAcct.Add(model);
            }


            #endregion

            #region 添加分类

            var advanceBaseModel = AdvancedBaseData.Create();

            advanceBaseModel.BaseName = "超威电池";
            advanceBaseModel.Account = curUser;
            advanceBaseModel.CreateDate = DateTime.Now;
            advanceBaseModel.LastUpdateDate = DateTime.Now;
            AdvancedBaseData.Add(advanceBaseModel);


            #endregion

            #region 添加商品

            using (new Product())
            {
                var model = Product.Create();
                model.ProductName = "超威48V20Ah";
                model.Account = curUser;
                model.Type = advanceBaseModel;
                model.CreateDate = DateTime.Now;
                model.LastUpdateDate = DateTime.Now;
                model.InitStock = model.CurStock = 0;
                Product.Add(model);
            }

            using (new Product())
            {
                var model = Product.Create();

                model.ProductName = "超威60V20Ah";
                model.Account = curUser;
                model.Type = advanceBaseModel;
                model.CreateDate = DateTime.Now;
                model.LastUpdateDate = DateTime.Now;
                model.InitStock = model.CurStock = 0;
                Product.Add(model);
            }

            #endregion

            #region 进货单

            var purBills = PurBills.Create();
            purBills.CreateDateTime = DateTime.Now;
            purBills.LastUpdateDateTime = DateTime.Now;
            purBills.User = curUser;
          

            //   CoreServiceFactory.CreatePurService().Add();

            #endregion

        }

        public class 操作员
        {
            //public void 
        }
       
        /// <summary>
        /// 测试销售订单，下单流程
        /// </summary>
        [TestMethod]
        public void TestOrderSale()
        {
            
            var radom = new Random(Product.List.Count);
            var radomCount = new Random(20);
            //客户队列
            var companyQueue = new Queue<Company>();

            InitDate();

            //创建几个客户
            var 株洲统一电瓶 = new Company() { CompanyName = "株洲统一电瓶" }.WantOrderProduct(Product.List[radom.Next()], radomCount.Next())
                .WantOrderProduct(Product.List[radom.Next()], radomCount.Next());
            var 株洲花城汽车修理厂 = new Company() { CompanyName = "株洲统一电瓶" }.WantOrderProduct(Product.List[radom.Next()], radomCount.Next());
            var 株洲摩托车市场蒋斌 = new Company() { CompanyName = "株洲摩托车市场蒋斌" }.WantOrderProduct(Product.List[radom.Next()], radomCount.Next());
            var 零售客户 = new Company() { CompanyName = "零售客户" }.WantOrderProduct(Product.List[radom.Next()], radomCount.Next());

            companyQueue.Enqueue(株洲统一电瓶);
            companyQueue.Enqueue(株洲花城汽车修理厂);
            companyQueue.Enqueue(株洲摩托车市场蒋斌);
            companyQueue.Enqueue(零售客户);

            var 操作员 = CoreServiceFactory.CreateUserService().SignIn("admin", "admin");

            var machines = new Machines(操作员);

            foreach (var company in companyQueue)
            {
                machines.OrderSaleBill(company);
            }

            //foreach (var company in companyQueue)
            //{
                
            //}

            #region 添加销售订单

            var order = SaleOrderBills.Create();
            order.CreateDateTime = DateTime.Now;
            order.LastUpdateDateTime = DateTime.Now;
            order.GetAmount = 300;//收了定金300元
            order.Company = 株洲统一电瓶;
            order.User = curUser;

            order.SaleOrderBillItems.Add(new SaleOrderBillItems(Product.Get(0))
            {
                ItemCount = 1,
                ShowItemCount = 1,
                ShowPrice = 540,
                Price = 520,
                Amount = 520
            });

            order.SaleOrderBillItems.Add(new SaleOrderBillItems(Product.Get(1))
            {
                ItemCount = 1,
                ShowItemCount = 1,
                ShowPrice = 700,
                Price = 650,
                Amount = 520
            });

            order.BillCode = CoreServiceFactory.CreateBillCodeService().GenerationBillCode(BillCodeService.BillCodeType.销售订单);
            order.Remark = "DH:0000021";
            order.BillState = BillStateEnum.执行中;
            order.SettleAcct = SettleAcct.GetSettleAcctByName("现金");
            order.BillAmount = order.SaleOrderBillItems.Sum(x => x.Amount);
            CoreServiceFactory.CreateSaleOrderService().Add(order);

            var testModel = FlowBills.List.LastOrDefault();
            Assert.IsTrue(testModel != null && testModel.CurAmonut > 0);
            var settleModel = SettleAcct.List.FirstOrDefault(x => x.SettleAcctName == "现金");
            Assert.IsTrue(settleModel != null && settleModel.CurAmount > 0);

            //var testProduct = Product.List.FirstOrDefault();
            //Assert.IsTrue(testProduct != null && testProduct.CurStock==9);

            #endregion

            #region 配送订单产品

            var sendSaleOrderProductList = new List<ProductSendObj>();
            //装配已经确认要发货的订单产品
            var assembleProductService = CoreServiceFactory.CreateSaleService().AssembleProduct(order, sendSaleOrderProductList);

            #endregion

            #region 直接进行销售
            //进行销售，结算处理
            assembleProductService.Sale();
                
            #endregion

        }

        [TestMethod]
        public void TestOrder()
        {   

            #region 修改销售订单

            var order = SaleOrderBills.Get(0);

            order.LastUpdateDateTime = DateTime.Now;

            order.Company = new Company() { CompanyName = "株洲统一电瓶" };

            order.SaleOrderBillItems.Add(new SaleOrderBillItems(Product.Get(0))
            {
                ItemCount = 1,
                ShowItemCount = 1,
                ShowPrice = 540,
                Price = 520,
                Amount = 520
            });

            order.SaleOrderBillItems.Add(new SaleOrderBillItems(Product.Get(1))
            {
                ItemCount = 1,
                ShowItemCount = 1,
                ShowPrice = 700,
                Price = 650,
                Amount = 520
            });

            order.BillCode = "XSDD20150400001";
            order.Remark = "DH:0000021";
            order.SettleAcct = SettleAcct.GetSettleAcctByName("现金");
            order.BillState = BillStateEnum.执行中;
            order.BillAmount = order.SaleOrderBillItems.Sum(x => x.Amount);
            order.GetAmount = order.BillAmount;
            SaleOrderBills.Add(order);

            #endregion

        }
    }
}
